{extend name="share:layout"/}
{block name="title"}公共方法{/block}
{block name="body"}
<h4>公共方法</h4>
<textarea readonly style="width: 100%; height: 750px; font-family: &quot;Courier New&quot;; font-size: 12px; line-height: 1.5;">
//打印数组
function dd($data){
    if(empty($data)){
        echo "<pre>";
        var_dump($data);
    }
    echo "<pre>";
    print_r($data);
}
//请求返回json
function res($code = 0, $data = '',$message = ''){
    //调用extra/code配置文件中定义好的常量
    $res = [
        'code' => config("code.".$code)['code'],
        'message' => config("code.".$code)['message'],
        'data' => $data,
    ];
    if(!empty($message)){
        $res['message'] = $message;
    }
    return json($res);
}
//请求返回
function returnstr($code = 0, $message = ''){
    $res = config("code.".$code)['message'];
    if(!empty($message)){
        $res = $message;
    }
    return $res;
}
//递归
function recursive($list, $parent_id=0){
    $arr = array();
    foreach($list as $l){
        if($l['parent_id'] == $parent_id){
            $arr[] = $l;
            $child = recursive_menu($list, $l['menu_id']);
            if(is_array($child)){
                $arr = array_merge($arr, $child);
            }
        }
    }
    return $arr;
}
//删除数据
function delete_data($where, $table){
    if(is_array($where)){
        $res = db($table)->where($where)->delete();
        return $res;
    }
    $res = db($table)->delete($where);
    return $res;
}
//更新某一个字段
function update_data($table, $where, $setField){
    $data = db($table)
        ->where($where)
        ->setField($setField);
    return $data;
}
//查询表里一条数据
function find_data($table, $where){
    $data = db($table)
        ->where($where)
        ->find();
    return $data;
}
//查询表里全部数据
function select_data($table, $where){
    $data = db($table)
        ->where($where)
        ->select();
    return $data;
}
//统计数据
function count_data($table, $where){
    $data = db($table)
        ->where($where)
        ->count();
    return $data;
}
</textarea>
{/block}